import axios, { AxiosInstance } from "axios";
import { getToken } from "./auth";
import exportVue from "@/assets/modules/exportVue";

const requestHost = "/platform-api";
const requestServe: any = {
  lowCode: "/updt-lowcode",
  upms: "/updt-upms",
  wepush: "/updt-wepush",
  datafactory: "/updt-datafactory",
  gis: "/updt-gis",
  third: "/updt-house-third",
  auth: "/updt-auth",
  file: "/updt-file"
};

const instance: AxiosInstance = axios.create();
instance.defaults.timeout = 3000000;
instance.defaults.withCredentials = false;

instance.interceptors.request.use(
  config => {
    const token = getToken();
    if (token) {
      config.headers.Authorization = token;
    }
    return config;
  },
  error => {
    // Do something with request error
    console.log(error); // for debug
    Promise.reject(error);
  }
);

instance.interceptors.response.use(
  response => {
    if (response && response.data && response.data.code == "401") {
      exportVue
        .$confirm("您已登录失效, 请前去登录?", "提示!", {
          confirmButtonText: "去登录",
          type: "warning",
          showClose: false,
          showCancelButton: false
        })
        .then(() => {
          const win: any = window;
          window.location.replace(win.user.loginUrl);
        });
    }
    return response;
  },
  error => {
    if (error.response) {
      console.log(error.response);
    } else {
      console.log(error);
    }
  }
);
/**
 * 自动登录获取token,用来开发
 */
export function getLoginToken(param: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(`${requestHost}${requestServe.auth}/oauth/token`, param)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
/**
 * 底图资源层列表（2/3D)
 */
export function getInitMapConfig(param: any) {
  return new Promise((resolve, reject) => {
    instance
      .get(
        `${requestHost}${requestServe.gis}/gis-resource/org-tree-base-map/${param.cateSfid}`,
        { params: param }
      )
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}

/**
 * 查询网格树列表
 */
export function getGridList(param: any) {
  return new Promise((resolve, reject) => {
    instance
      .get(`${requestHost}${requestServe.lowCode}/wangge/info/getGridList`, {
        params: param
      })
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}

/**
 * 查询网格信息
 */
export function getGridInfo(data: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(
        `${requestHost}${requestServe.lowCode}/wangge/info/getXingZhengAreaTJ`,
        data
      )
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
/**
 * 查询楼房信息
 */
export function getBuildingInfo(data: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(
        `${requestHost}${requestServe.lowCode}/lf/xx/findBuilddingInfoStatistics`,
        data
      )
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
/**
 * 查询租赁房源入统数
 */
export function getHouseRentList(data: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(`${requestHost}${requestServe.lowCode}/lf/xx/findZlfyrtsList`, data)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
/**
 * 查询房源发布数
 */
export function getHousePublishList(data: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(`${requestHost}${requestServe.lowCode}/lf/xx/findFyfbsList`, data)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
/**
 * 查询保障性住房数
 */
export function getSupportHouseList(data: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(`${requestHost}${requestServe.lowCode}/lf/xx/findBzxzfsList`, data)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
/**
 * 查询租赁合同
 */
export function getTenancyContract(data: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(
        `${requestHost}${requestServe.lowCode}/zl/ht/findLeaseContractStatistics`,
        data
      )
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
/**
 * 查询租赁合同列表
 */
export function getTenancyContractList(data: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(`${requestHost}${requestServe.lowCode}/zl/ht/getZlhtTc`, data)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
/**
 * 查询项目信息
 */
export function getProjectInfo(data: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(
        `${requestHost}${requestServe.lowCode}/xm/xx/findXmxxStatistics`,
        data
      )
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
/**
 * 查询项目列表
 */
export function getProjectList(data: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(`${requestHost}${requestServe.lowCode}/xm/xx/list`, data)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
/**
 * 查询项目点位信息
 */
export function getZlxmData() {
  return new Promise((resolve, reject) => {
    instance
      .post(`${requestHost}${requestServe.lowCode}/xm/xx/dwxx`)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
/**
 * 查询从业主体信息
 */
export function getEmployeeInfo(data: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(
        `${requestHost}${requestServe.lowCode}/cyzt/xx/findPractitionerInfoStatistics`,
        data
      )
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
/**
 * 查询租赁企业
 */
export function getZlqyInfo(data: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(
        `${requestHost}${requestServe.lowCode}/cyzt/xx/getLeaseEnterpriseDetail`,
        data
      )
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}

/**
 * 查询经纪机构
 */
export function getJjjgInfo(data: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(
        `${requestHost}${requestServe.lowCode}/cyzt/xx/getDeconomyInstitutionDetail`,
        data
      )
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}

/**
 * 获取数据仓库
 */
export function getDataStore(data: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(
        `${requestHost}${requestServe.datafactory}/datastore/view/list`,
        data
      )
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}

/**
 * 查询外业情况
 */
export function getFieldSituation(data: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(
        `${requestHost}${requestServe.lowCode}/wy/qk/findWyqkStatistics`,
        data
      )
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}

/**
 * 查询房屋大数据全域核查各区情况
 */
export function getGlobalCheck(data: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(
        `${requestHost}${requestServe.lowCode}/wy/qk/getWyqkFwdsjQyhcGqqk`,
        data
      )
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}

/**
 * 坐落关键字查询楼盘
 */
export function getFloorId(param: any) {
  return new Promise((resolve, reject) => {
    instance
      .get(`${requestHost}${requestServe.lowCode}/lp/xx/findLpzlList`, {
        params: param
      })
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}

/**
 * 查询楼盘基本信息
 */
export function getFloorInfo(param: any) {
  return new Promise((resolve, reject) => {
    instance
      .get(`${requestHost}${requestServe.lowCode}/lp/xx/getLpjbxx`, {
        params: param
      })
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}

/**
 * 查询房源及其他信息
 */
export function getHouseOther(param: any) {
  return new Promise((resolve, reject) => {
    instance
      .get(`${requestHost}${requestServe.lowCode}/lp/xx/getFyjqt`, {
        params: param
      })
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}

/**
 * 查询楼盘房源信息
 */
export function getFloorHouse(param: any) {
  return new Promise((resolve, reject) => {
    instance
      .get(`${requestHost}${requestServe.lowCode}/lp/xx/getLpfyxx`, {
        params: param
      })
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}

/**
 * 查询全屋
 */
export function getWholeHouse(param: any) {
  return new Promise((resolve, reject) => {
    instance
      .get(`${requestHost}${requestServe.lowCode}/lp/xx/getFwxx`, {
        params: param
      })
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}

/**
 * 查询房管员列表
 */
export function getGridmanList(data: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(
        `${requestHost}${requestServe.lowCode}/wanggeyuan/getWangGeYuanListBy`,
        data
      )
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}

/**
 * 查询房管员详情
 */
export function getGridmanDetail(data: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(
        `${requestHost}${requestServe.lowCode}/wanggeyuan/getWanggeYuanDetail`,
        data
      )
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}

/**
 * 查询房管员工作情况
 */
export function getTaskHandling(data: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(`${requestHost}${requestServe.lowCode}/wanggeyuan/getGzqkTJ`, data)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}

/**
 * 查询房管员工作情况
 */
export function getTaskHouseRent(data: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(
        `${requestHost}${requestServe.lowCode}/wanggeyuan/getQyhcfwzs`,
        data
      )
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}

/**
 * 查询房管员合同备案
 */
export function getGridmanTenancyContract(data: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(`${requestHost}${requestServe.lowCode}/wanggeyuan/getHtbazs`, data)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}

/**
 * 查询房管员房源发布审核数
 */
export function getGridmanHousePublish(data: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(`${requestHost}${requestServe.lowCode}/wanggeyuan/getFyfbshs`, data)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
/********************监控大屏*********************/
/**
 * 获取监控大屏的项目列表
 */
export function getJkxmList(data: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(`${requestHost}${requestServe.lowCode}/xmgl/getJkxmList`, data)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
/**
 * 获取历史巡查日历
 */
export function getXclsRl(data: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(`${requestHost}${requestServe.lowCode}/xccc/getXclsRl`, data)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
/**
 * 线上巡查项目信息
 */
export function getXsxcXmxx(ywid: string) {
  return new Promise((resolve, reject) => {
    instance
      .get(
        `${requestHost}${requestServe.lowCode}/xccc/getXsxcXmxx?ywid=${ywid}`
      )
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
/**
 * 获取视频token
 */
export function getMonitorToken(param: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(`${requestHost}${requestServe.third}/monitor/getAccessToken`, param)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
/**
 * 保活接口，登陆之后，需要30S调用一次向后台进行保活
 */
export function monitorKeepalive(param: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(`${requestHost}${requestServe.third}/monitor/keepalive`, param)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
/**
 * 添加业务数据
 */
export function createBusiDataRelation(param: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(`${requestHost}${requestServe.lowCode}/busiDataRelation`, param)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
/**
 * 更新业务数据
 */
export function updateBusiDataRelation(param: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(`${requestHost}${requestServe.lowCode}/busiDataRelation/update`, param)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
/**
 * 上传附件
 */
export function uploadFile(param: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(
        `${requestHost}${requestServe.file}/repository/file/mongo/upload`,
        param
      )
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
/**
 * 获取编码器
 */
export function buildCode(code: any) {
  return new Promise((resolve, reject) => {
    instance
      .get(`${requestHost}${requestServe.upms}/baseEncoder/buildCode/${code}`)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
/**
 * 房管员查看历史
 */
export function getWgyCkls(data: any) {
  return new Promise((resolve, reject) => {
    instance
      .post(`${requestHost}${requestServe.lowCode}/wanggeyuan/getWgyCkls`, data)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}

/**
 * 房管员查看历史详情
 */
export function getWgyCklsXq(xqid: any) {
  return new Promise((resolve, reject) => {
    instance
      .get(
        `${requestHost}${requestServe.lowCode}/wanggeyuan/getWgyCklsXq?xqid=${xqid}`
      )
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}

/**
 * 从业主体信息点位
 */
export function getCyztdwxx() {
  return new Promise((resolve, reject) => {
    instance
      .post(`${requestHost}${requestServe.lowCode}/cyzt/xx/cyztdwxx`)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}

/**
 * 经纪机构信息点位
 */
export function getJjjgdwxx() {
  return new Promise((resolve, reject) => {
    instance
      .post(`${requestHost}${requestServe.lowCode}/cyzt/xx/jjjgdwxx`)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
export function getSzqy() {
  return new Promise((resolve, reject) => {
    instance
      .post(
        `${requestHost}${requestServe.lowCode}/dyWgapply/getRyXzq`
      )
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
